/**
 * 컨텐츠 관련 js
 * 
 * @author gudals
 * @since 2013-07-19
 */

$(function() {
    Conts.init();
});

var Conts = {
    aUploads: [],
        
    init: function () {},
    
    /**
     * 등록
     */
    /*
    add: function () {
        if (!$('#contents_desc').val().trim()) {
            return false;
        }
        
        var params = 'c=main&m=add&'+$('#cForm').serialize();
        
        $.ajax({
            type: 'POST',
            url: '/cont/',
            dataType: 'json',
            data: params,
            success:function(data) {
                if (data.code == 200) {
                    alert('정상적으로 처리 되었습니다.');
                    window.location.reload();
                    
                } else {
                    alert('잘못 되었습니다. 다시 시도해 주세요. [ code : '+data.code+']');
                }
            }
        });
    },
    */
    
    regist: function()
    {
        var options = {
            target: '#form_output',
            url: '/cont/?c=main&m=add',
            dataType: 'json',
            beforeSubmit: Conts.reqRegist,
            success: Conts.resRegist
        };
        
        /*
        $('#regForm').attr('action', '/cont/?c=main&m=add');
        $('#regForm').submit();
        return false;
        */
        
        $('#regForm').ajaxSubmit(options);
    },
    
    reqRegist: function(formData, jqForm, options)
    {
        if ($('#contents_desc').val() == '') {
            $('#contents_desc').focus();
            return false;
        }
        
        Base.spinOn();
        
        return true;
    },
    
    resRegist: function(responseText, statusText, xhr, $form)
    {
        Base.spinOff();
        
        if (responseText.code == 200) {
            alert(Base.msg[200]);
            top.location.reload();
            
        } else {
            alert(Base.msg[201]+' '+responseText.code+' : '+responseText.msg);
        }
        
        return false;
    },
    
    /**
     * 댓글 리스트 html : 댓글 등록 후 바로 실행
     */
    getCommentHtml: function(iContentsNum)
    {
        if (iContentsNum == undefined) return false;
        
        var params = 'c=main&m=commentList&contents_num='+iContentsNum;
        
        $.ajax({
            type: 'POST',
            url: '/cont/',
            dataType: 'json',
            data: params,
            success:function(data) {
                if (data.code == 200) {
                    $('#comment_list_'+iContentsNum).html(data.res);
                    
                } else {
                    alert(Base.msg[201]+' [ code : '+data.code+']');
                }
            }
        });
    },
    
    /**
     * 댓글 등록
     */
    regComment: function(iContentsNum)
    {
        if (!$('#comment_desc_'+iContentsNum).val().trim() || iContentsNum == undefined) {
            $('#comment_desc_'+iContentsNum).focus();
            return false;
        }
        
        var params = 'c=main&m=addComment&contents_num='+iContentsNum+'&'+$('#cmtForm_'+iContentsNum).serialize();
        
        $.ajax({
            type: 'POST',
            url: '/cont/',
            dataType: 'json',
            data: params,
            success:function(data) {
                if (data.code == 200) {
                    $('#comment_desc_'+iContentsNum).prop('value','');
                    Conts.getCommentHtml(iContentsNum);
                    
                } else {
                    alert(Base.msg[201]+' [ code : '+data.code+']');
                }
            }
        });
    }
};